package com.etone.smartAudit.service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.etone.smartAudit.domain.Admin;
import com.etone.smartAudit.domain.AuditExceptionDownload;
import com.etone.smartAudit.error.CustomParameterizedException;
import com.etone.smartAudit.mapper.AuditExceptionDownloadMapper;
import com.etone.smartAudit.utils.DateUtil;
import com.etone.smartAudit.utils.StringUtil;
import com.etone.smartAudit.utils.ZipUtil;
import com.etone.smartAudit.vo.FileVO;
import com.etone.smartAudit.vo.PagerVO;
import com.etone.smartAudit.vo.auditException.AuditExceptionDownloadVO;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.AllArgsConstructor;
import org.elasticsearch.common.time.DateUtils;
import org.joda.time.format.DateTimeFormat;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author ：张任豪
 * @date ：2022/3/1 17:34
 * @description：
 */
@Service
public class AuditExceptionDownloadService extends ServiceImpl<AuditExceptionDownloadMapper, AuditExceptionDownload> {

    @Value("${audit.filePath}")
    private String filePath;

    @Autowired
    private FileUploadService fileUploadService;

    public void download(Integer id, HttpServletResponse response) {
        AuditExceptionDownload auditExceptionDownload = baseMapper.selectById(id);
        if (auditExceptionDownload == null) {
            throw new CustomParameterizedException("文件不存在!");
        }
        String fileName = auditExceptionDownload.getRemark();
        File file = new File(filePath + auditExceptionDownload.getPath());
        fileUploadService.getFile(file, response, fileName);
    }

    public PagerVO<AuditExceptionDownloadVO> voList(int page, int pageSize, Integer state, Integer type, String remark, String startTime, String endTime, Admin admin) {
        Page<AuditExceptionDownloadVO> auditExceptionDownloadVOPage = PageHelper.startPage(page, pageSize).doSelectPage(() ->
                baseMapper.list(state, type, remark, startTime, endTime, admin.isAdmin() ? null : admin.getId()));
        return PagerVO.of(auditExceptionDownloadVOPage);
    }

    public void delete(List<Integer> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            throw new CustomParameterizedException("文件集合不能为空！");
        }
        //删除文件
        List<String> urls = new ArrayList<>();
        for (Integer id : ids) {
            AuditExceptionDownload auditExceptionDownload = baseMapper.selectById(id);
            urls.add(auditExceptionDownload.getUrl());
        }
        fileUploadService.deleteFileByURL(urls, true, true);
        //删除记录
        baseMapper.deleteBatchIds(ids);
    }

    public void batchDownload(List<Integer> ids, HttpServletResponse response) {
        if (CollectionUtils.isEmpty(ids)) {
            throw new CustomParameterizedException("文件集合不能为空！");
        }
        List<FileVO> fileVOS = new ArrayList<>();
        for (Integer id : ids) {
            AuditExceptionDownload auditExceptionDownload = baseMapper.selectById(id);
            fileVOS.add(FileVO.builder().url(auditExceptionDownload.getPath()).originName(auditExceptionDownload.getRemark()).build());
        }
        ZipUtil.downloadFileZip(fileVOS, "报表《整合》" + DateUtil.parseDate2String(new Date(), "yyyyMMddHHmmss") + ".zip", response);
    }
}
